package zombie;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class TestZombie extends JPanel implements KeyListener, Runnable {
    public static BufferedImage zombie;
    private Image offScreenImage = null;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    private static Zombie zombie1;
    static { // 静态代码块，初始化图片资源
        try {
            zombie1 = new Zombie();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public TestZombie() {
        JFrame frame=new JFrame("这是窗体的名字");
        frame.setSize(WIDTH, HEIGHT);
        frame.addKeyListener(this);
        frame.add(this);
        frame.setVisible(true);
        Thread thread = new Thread(this);
        thread.start();
    }
    @Override
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = createImage(800, 600);
        }
        Graphics graphics = offScreenImage.getGraphics();
        graphics.fillRect(0, 0, 800, 600);
        graphics.drawImage(zombie1.getShow(), zombie1.getX(), zombie1.getY(), this);
        g.drawImage(offScreenImage, 0, 0, this);
    }

    @Override
    protected void processKeyEvent(KeyEvent e) {
        System.out.println(e.getKeyCode());
    }

    public static void main(String[] args) {
        TestZombie testZombie = new TestZombie();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println(e.getKeyCode());
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println(e.getKeyCode());
    }


    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(500);
                zombie1.bofang();
                repaint();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
